Option Strict Off
Option Explicit On
Friend Class RSA
	'FIXIT: Use Option Explicit to avoid implicitly creating variables of type Variant         FixIT90210ae-R383-H1984
	
	Private Const base64 As String = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
	Private Function Encode_Base(ByRef decryptedtext As String) As String
		'FIXIT: Declare 'C1' and 'C2' with an early-bound data type                                FixIT90210ae-R1672-R1B8ZE
		Dim C1, C2 As Object
		Dim C3 As Short
		Dim w1 As Short
		Dim w2 As Short
		Dim w3 As Short
		Dim w4 As Short
		Dim n As Short
        Dim retry As String = String.Empty
		For n = 1 To Len(decryptedtext) Step 3
			'UPGRADE_WARNING: Couldn't resolve default property of object C1. Click for more: 'ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
			C1 = Asc(Mid(decryptedtext, n, 1))
			'UPGRADE_WARNING: Couldn't resolve default property of object C2. Click for more: 'ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
			C2 = Asc(Mid(decryptedtext, n + 1, 1) & Chr(0))
			C3 = Asc(Mid(decryptedtext, n + 2, 1) & Chr(0))
			'UPGRADE_WARNING: Couldn't resolve default property of object C1. Click for more: 'ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
			w1 = Int(C1 / 4)
			'UPGRADE_WARNING: Couldn't resolve default property of object C2. Click for more: 'ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
			w2 = CShort(C1 And 3) * 16 + Int(C2 / 16)
			If Len(decryptedtext) >= n + 1 Then w3 = CShort(C2 And 15) * 4 + Int(C3 / 64) Else w3 = -1
			If Len(decryptedtext) >= n + 2 Then w4 = C3 And 63 Else w4 = -1
			retry = retry & mimeencode(w1) & mimeencode(w2) & mimeencode(w3) & mimeencode(w4)
		Next 
		Encode_Base = retry
	End Function
    Public Function Encode(ByRef decryptedtext As String) As String
        Encode = String.Empty
        Dim temp As String
        Dim mark As String 'the index code which indicating the postion of the useful string. the length of mark is always 8
        Dim i As Short
        Dim k As Integer
        'FIXIT: Non Zero lowerbound arrays are not supported in Visual Basic .NET                  FixIT90210ae-R9815-H1984
        'UPGRADE_WARNING: Lower bound of array binary_Renamed was changed from 1 to 0. Click for more: 'ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?keyword="0F1C9BE1-AF9D-476E-83B1-17D43BECFF20"'
        'UPGRADE_NOTE: binary was upgraded to binary_Renamed. Click for more: 'ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?keyword="A9E4979A-37FA-4718-9994-97DD76ED70A7"'
        Dim binary_Renamed(24) As Boolean ' the array of the boolean variable. their usage is the same as the mark
        Dim index As Short
        temp = Encode_Base(decryptedtext) 'base encode string whose length is not certain
        If Len(temp) > 24 Then
            Encode = ""
            Exit Function
        End If
        For i = 1 To 24
            binary_Renamed(i) = False
        Next i


        k = 0

        For i = 1 To Len(temp)
            index = CShort(Rnd() * 23) + 1
            While binary_Renamed(index) = True
                index = CShort(Rnd() * 23) + 1
            End While
            binary_Renamed(index) = True
            k = k + 2 ^ (index - 1)
        Next i
        index = 1
        Dim j As Short
        For i = 1 To 24
            If binary_Renamed(i) = True Then

                Encode = Encode & Mid$(temp, index, 1)
                index = index + 1
            Else
                j = Rnd() * 51
                Encode = Encode & Chr(IIf(j > 25, j - 25 + Asc("A"), Asc("a") + j))
            End If

        Next i
        mark = ""
        For index = 1 To 8
            i = k - (k \ 10) * 10
            temp = IIf(Rnd() > 0.5, Chr(i + Asc("A")), Chr(i + Asc("a")))
            mark = temp & mark
            k = k \ 10
        Next index
        Encode = mark & Encode




    End Function
	
	
	Public Function Decode(ByRef decryptedtext As String) As String
		Dim temp1 As String
		
		
		Dim i As Short
		Dim k As Integer
		
		If Len(decryptedtext) <> 32 Then 'invalid length of the code, exit
			Decode = ""
			Exit Function
		End If
		
		k = 0 'digit value of mark
		For i = 1 To 8
            k = k * 10 + CShort(Asc(UCase$(Mid$(decryptedtext, i, 1))) - Asc("A"))
		Next i
		
		
		

		'FIXIT: Non Zero lowerbound arrays are not supported in Visual Basic .NET                  FixIT90210ae-R9815-H1984
		'UPGRADE_WARNING: Lower bound of array binary_Renamed was changed from 1 to 0. Click for more: 'ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?keyword="0F1C9BE1-AF9D-476E-83B1-17D43BECFF20"'
		'UPGRADE_NOTE: binary was upgraded to binary_Renamed. Click for more: 'ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?keyword="A9E4979A-37FA-4718-9994-97DD76ED70A7"'
		Dim binary_Renamed(24) As Boolean
		
		For i = 1 To 24
			If k - (k \ 2) * 2 = 1 Then
				binary_Renamed(i) = True
			Else
				binary_Renamed(i) = False
			End If
			k = k \ 2
		Next i
		temp1 = ""
		For i = 1 To 24
			If binary_Renamed(i) = True Then
				'FIXIT: Replace 'Mid' function with 'Mid$' function                                        FixIT90210ae-R9757-R1B8ZE
				temp1 = temp1 & Mid(decryptedtext, i + 8, 1)
			End If
		Next i
		Decode = Decode_Base(temp1)
		
	End Function
	
	
	
    Private Function Decode_Base(ByRef a As String) As String
        Dim w1 As Short
        Dim w2 As Short
        Dim w3 As Short
        Dim w4 As Short
        Dim n As Short
        Dim retry As String = String.Empty
        For n = 1 To Len(a) Step 4
            w1 = mimedecode(Mid(a, n, 1))
            w2 = mimedecode(Mid(a, n + 1, 1))
            w3 = mimedecode(Mid(a, n + 2, 1))
            w4 = mimedecode(Mid(a, n + 3, 1))
            If w2 >= 0 Then retry = retry & Chr((w1 * 4 + Int(w2 / 16)) And 255)
            If w3 >= 0 Then retry = retry & Chr((w2 * 16 + Int(w3 / 4)) And 255)
            If w4 >= 0 Then retry = retry & Chr((w3 * 64 + w4) And 255)
        Next
        Decode_Base = retry
    End Function
	Private Function mimeencode(ByRef w As Short) As String
		If w >= 0 Then mimeencode = Mid(base64, w + 1, 1) Else mimeencode = ""
	End Function
	Private Function mimedecode(ByRef a As String) As Short
		If Len(a) = 0 Then mimedecode = -1 : Exit Function
		mimedecode = InStr(base64, a) - 1
	End Function
End Class